package com.itheima.sh.esdemo.demo;

import com.alibaba.fastjson.JSON;
import com.itheima.sh.esdemo.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 文档 搜索
 *
 * @作者 itcast
 * @创建日期 2021/5/12 18:28
 **/
public class EsDemo03 {
    RestHighLevelClient client;
    @Test
    public void matchAll() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery()); // 查询条件


        printResult(builder);
    }

    /**
     * note 分词字段         唱歌跳舞javaee     ==>  match
     */
    @Test
    public void match(){
        int page = 2;
        int pageSize = 3;

        SearchSourceBuilder builder = new SearchSourceBuilder();
//        builder.query(QueryBuilders.matchQuery("note","唱歌跳舞javaee"));
        builder.query(QueryBuilders.matchQuery("note","同学"));
        // 排序    排序字段    升序或降序
        builder.sort("age", SortOrder.DESC);
        // 可以使用多个字段排序
        builder.sort("gender", SortOrder.ASC);
        builder.from((page - 1)* pageSize);
        builder.size(pageSize);
        printResult(builder);
    }
    /**
     * age 分词字段         18     30
     */
    @Test
    public void range(){
        SearchSourceBuilder builder = new SearchSourceBuilder();

        builder.query(QueryBuilders.rangeQuery("age").gte(18).lte(30));

        printResult(builder);
    }
    /**
     * note 分词字段    同学   并且   年龄 >  20岁   并且   性别女同学
     */
    @Test
    public void bool(){
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 构建bool条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // note 分词字段    同学
        boolQueryBuilder.must(QueryBuilders.matchQuery("note","同学"));
        // age  年龄 >  20岁
        boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gt(20));
        // gender 性别女同学
        boolQueryBuilder.must(QueryBuilders.termQuery("gender","0"));
        builder.query(boolQueryBuilder);
        printResult(builder);
    }


    public void printResult(SearchSourceBuilder builder){
        // 1.创建searchRequest
        SearchRequest request = new SearchRequest("user");
        // 1.1 构建请求参数对象
        request.source(builder);
        // 2.调用搜索请求
        SearchResponse response = null;
        try {
            response = client.search(request, RequestOptions.DEFAULT);
            // 3.解析响应结果
            SearchHits hits = response.getHits(); // 得到命中结果
            System.out.println("查询到结果总数量: " + hits.getTotalHits().value);
            // 3.1 得到文档列表
            SearchHit[] docList = hits.getHits();
            for (SearchHit doc : docList) {
                System.out.println("文档得分: " + doc.getScore());


                String userJson = doc.getSourceAsString();
                User user = JSON.parseObject(userJson, User.class);
                System.out.println(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("查询出现异常"+ e.getMessage());
        }
    }

    @Before
    public void init(){
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.130", 9200, "http")));
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
